package cn.richard.home.view.activity

import androidx.lifecycle.Observer
import cn.richard.base.view.BaseActivity
import cn.richard.base.constant.RouterPath
import cn.richard.home.R
import cn.richard.home.wdget.PicListDecoration
import cn.richard.base.constant.EXTRA
import cn.richard.home.databinding.ActivityPicListBinding
import cn.richard.home.view.adapter.PicListAdapter
import cn.richard.home.viewmodel.PicListViewModel
import com.alibaba.android.arouter.facade.annotation.Route
import org.koin.androidx.viewmodel.ext.android.viewModel

@Route(path = RouterPath.Home.PicList)
class PicListActivity : BaseActivity<ActivityPicListBinding>() {

    private lateinit var categoryId: String

    private val viewModel: PicListViewModel by viewModel()

    private val picListAdapter = PicListAdapter()

    override fun getLayoutId(): Int = R.layout.activity_pic_list

    override fun initView() {
        categoryId = intent.getStringExtra(EXTRA.ID).toString()

        picListAdapter.loadMoreModule.setOnLoadMoreListener {
            getPicList()
        }
        mBinding.clvContent.adapter = picListAdapter
        mBinding.clvContent.addItemDecoration(PicListDecoration())
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.picturesLiveData.observe(this, Observer {
            if (it.success) {
                it.pictureList?.toList()?.let {
                        it1 -> picListAdapter.addData(it1)
                }
                picListAdapter.loadMoreModule.loadMoreComplete()
            } else {
                picListAdapter.loadMoreModule.loadMoreEnd()
            }
        })
    }

    override fun initData() {
       getPicList()
    }

    private fun getPicList() {
        viewModel.getPicList(categoryId, "new")
    }
}